//
// Created by hsj on 2022/3/10.
//

#include "t_initializer_list.h"
namespace t_initializer_list_test{

    double sum(initializer_list<double> il){
      double total = 0.0;
      for(auto p = il.begin(); p != il.end(); p++){
          total += *p;
      }
      return total;
    }

    double average(const initializer_list<double> &il){
        double total = 0.0;
        unsigned long long int size = il.size();
        double ave = 0.0;
        if(size > 0){
            for(auto p = il.begin(); p != il.end(); p++){
                total += *p;
            }
            ave = total / size;
        }
        return ave;
    }

    void test(){
      cout<<"sum({1,2,3}):"<<sum({1,2,3})<<endl;
      cout<<"average({1,2,3}):"<<average({1,2,3})<<endl;
      cout<<"*******************************************"<<endl;

      initializer_list<double> list_all = {1,2,3,4,5,6};
      cout<<"list_all sum:"<<sum(list_all)<<endl;
      cout<<"list_all average:"<<average(list_all)<<endl;
      cout<<"*******************************************"<<endl;

      auto val = 18;
      cout<<val<<endl;
    }
}